package cron

import (
	"fiber/app/models"
	"fiber/global"
	"log"
	"sync"

	"gorm.io/gorm/clause"
)

var l sync.Mutex

// 测下db
func TestDb() {
	db := global.DB

	user := models.User{}

	l.Lock()
	defer l.Unlock()

	tx := db.Begin()

	// 新版写法
	if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&user).Error; err != nil {
		tx.Rollback()
		log.Println(err)
		return
	}

	num := user.Num + 1
	log.Println(num)

	if err := tx.Model(&user).Update("num", num).Error; err != nil {
		tx.Rollback()
		log.Println(err)
		return
	}
	if err := tx.Commit().Error; err != nil {
		tx.Rollback()
		log.Println(err)
		return
	}

}
